import {createRouter, createWebHistory} from 'vue-router'
import {isLogin} from "@/utils/auth.ts";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/401',
      name: 'NotAuthed',
      component: () => import('../views/NotAuthed.vue'),
    },
    {
      path: '/404',
      name: 'NotFound',
      component: () => import('../views/NotFound.vue'),
    },
    // 未知路由重定向
    {
      path: '/:pathMatch(.*)',
      redirect: '/404',
    },
    {
      path: '/',
      name: 'Index',
      component: () => import('@/views/LoginPage.vue'),
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/LoginPage.vue'),
    },
  ],
})
//拦截器
router.beforeEach((to, from, next) => {
  const allowPaths: string[] = [
    '/',
    '/login',
    '/401',
    '/403',
    '/404',
  ];
  if (!allowPaths.includes(to.path)) {
    if (!isLogin()) {
      next('/401');
      return false;
    }
  }
  next()
})
export default router
